package com.wanmait.movies.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import com.wanmait.movies.util.DBHelper;
import com.wanmait.movies.vo.Actor;
import com.wanmait.movies.vo.MovieLanguage;
import com.wanmait.movies.vo.MovieType;

public class MovieTypeDao {

	private MovieTypeDao() {
	};

	private static MovieTypeDao movieTypeDao = new MovieTypeDao();

	public static MovieTypeDao getDao() {
		return movieTypeDao;
	}
	//修改类型
		 public void updateType(String name,int id){
				
				Connection conn = DBHelper.getDBHelper().getconn();
				PreparedStatement ps =null;
				
				String sql = "UPDATE movie_type set movie_type=? where id=? ";
				try {
					ps =conn.prepareStatement(sql);
					ps.setString(1, name);
					ps.setInt(2, id);
				
					ps.executeUpdate();
					
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				finally {
					DBHelper.getDBHelper().close(null, ps, conn);
				}

		   }
	//通过id获得
	   public MovieType findById(int id){
			
			Connection conn = DBHelper.getDBHelper().getconn();
			PreparedStatement ps =null;
			ResultSet rs =null;
			
			String sql = "select * from movie_type where id=?";
			try {
				ps =conn.prepareStatement(sql);
				ps.setInt(1, id);
				
				rs=ps.executeQuery();
				if(rs.next()) {
				
				MovieType a= new MovieType();
				a.setMovieType(rs.getString("movie_type"));
				a.setId(id);
				
				return a;
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			finally {
				DBHelper.getDBHelper().close(rs, ps, conn);
			}
			return null;
	   }
	//删除演员
	   public void deleteById(int id){
			
			Connection conn = DBHelper.getDBHelper().getconn();
			PreparedStatement ps =null;
			
			String sql = "delete from movie_type where id=?";
			try {
				ps =conn.prepareStatement(sql);
				ps.setInt(1, id);
			
				ps.executeUpdate();
				
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			finally {
				DBHelper.getDBHelper().close(null, ps, conn);
			}

	   }

	// 获得电影类型列表
	public ArrayList<MovieType> getTypeList() {
		ArrayList<MovieType> list = new ArrayList<MovieType>();

		Connection conn = DBHelper.getDBHelper().getconn();
		PreparedStatement ps = null;
		ResultSet rs = null;

		String sql = "select * from movie_type";

		try {
			ps = conn.prepareStatement(sql);

			rs = ps.executeQuery();

			while (rs.next()) {
				MovieType movieType = new MovieType();

				movieType.setId(rs.getInt("id"));
				movieType.setMovieType(rs.getString("movie_type"));

				list.add(movieType);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			DBHelper.getDBHelper().close(rs, ps, conn);
		}
		return list;
	}

	// 添加新类型
	public void add(String type) {

		Connection conn = DBHelper.getDBHelper().getconn();
		PreparedStatement ps = null;
		ResultSet rs = null;

		String sql = "insert into movie_type(movie_type) values(?)";
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, type);

			ps.executeUpdate();

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			DBHelper.getDBHelper().close(rs, ps, conn);
		}
	}
	
	//通过id获得类型
	   public MovieType getType(int id){
			
		   MovieType type = new MovieType();
			Connection conn = DBHelper.getDBHelper().getconn();
			PreparedStatement ps =null;
			ResultSet rs =null;
			
			String sql = "select * from movie_type where id=?";
			try {
				ps =conn.prepareStatement(sql);
				ps.setInt(1, id);
				
			while(rs.next()) {
				type.setMovieType(rs.getString("movie_type"));
				type.setId(id);
			}	
				
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			finally {
				DBHelper.getDBHelper().close(rs, ps, conn);
			}
			return type;
	}
}
